package com.zuqiuzhandui.liveing.view.login;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.code19.library.GsonUtil;
import com.code19.library.L;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.zuqiuzhandui.R;
import com.zuqiuzhandui.liveing.base.BaseActivity;
import com.zuqiuzhandui.liveing.util.UpdataUtil;
import com.zuqiuzhandui.liveing.view.choose.ChooseActivity;
import com.zuqiuzhandui.testing.myhelper.util.ToastUtils;

import org.lzh.framework.updatepluginlib.UpdateBuilder;
import org.lzh.framework.updatepluginlib.callback.UpdateDownloadCB;

import java.io.File;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements LoginContract.View, IWXAPIEventHandler {
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.bt_login)
    Button btLogin;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.activity_login)
    RelativeLayout activityLogin;
    @BindView(R.id.tv_weixin_login)
    TextView tvWeixinLogin;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;

    LoginContract.Presenter loginPresenter;
    private String wxLoginState;

    @Override
    public void setViewId() {
        setContentView(R.layout.activity_login);
    }


    @OnClick({R.id.tv_get_code, R.id.bt_login, R.id.tv_weixin_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                //获取验证码
                clickGetCode();
                break;
            case R.id.bt_login:
                clcikLogin();
                //登录
                break;
            case R.id.tv_weixin_login:
                //微信登录
                weixinLogin();
                break;
        }
    }

    private void clcikLogin() {
        loginPresenter.requestLogin(etMobile.getText().toString().trim(), etCode.getText().toString().trim());
    }

    @Override
    public void initView() {
        loginPresenter = new LoginPresenter(this);
        initToolbar();
        UpdataUtil.CheckUpdate(this);
    }


    private void clickGetCode() {
        loginPresenter.requestGetCode(etMobile.getText().toString().trim());
    }

    @Override
    public void showSuccessGetCode() {
        tvGetCode.setEnabled(false);
        timer.start();
    }

    @Override
    public void showSuccessLogin() {
        //登录成功
        toLastView(ChooseActivity.class);
        finish();
    }

    @Override
    protected void onDestroy() {
        timer.cancel();
        super.onDestroy();
    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {
        this.loginPresenter = presenter;
    }

    @Override
    public boolean isActive() {
        return isActive;
    }


    @Override
    public void showTip(String message) {
        ToastUtils.showToast(this, message);
    }


    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            tvGetCode.setText(millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            tvGetCode.setEnabled(true);
            tvGetCode.setText("发送验证码");
        }
    };


    private void weixinLogin() {
        IWXAPI wxapi = WXAPIFactory.createWXAPI(getApplicationContext(), WEIXIN_APP_ID, true);
        final SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";   //拉取个人信息的授权域
        wxLoginState = WEIXIN_APP_SECRET + new Random().nextInt(1000);
        req.state = wxLoginState;
        wxapi.sendReq(req);
    }


    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        L.d("微信返回的错误码是" + baseResp);
        SendAuth.Resp resp = (SendAuth.Resp) baseResp;
        L.d(GsonUtil.objectToJson(resp));
    }
}
